import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-applets',
	templateUrl: './applets.component.html',
})
export class AppletsComponent implements OnInit {
	validatorOptions = {
		excluded:[":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			configJson:{
				validators: {
					notEmpty: {
						message: '请输入配置信息'
					}
				}
			},
			appletsName:{
				validators: {
					notEmpty: {
						message: '请输入程序名字'
					}
				}
			},
			describe:{
				validators: {
					notEmpty: {
						message: '请输入描述'
					}
				}
			},
		}
	}
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	tableParam:any = {
		searchKey: ''
	}
	formType: any=''
	appletsFrom:any={
		id: '',
		configJson: '',
		describe: '',
		appletsName: ''
	}
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	ngOnInit() {
		this.load();
	}
	//table刷新
    refresh() {
    	this.load();
    }
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
    //分页回调
    paginate(e){
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:common.stringTrim(this.tableParam.searchKey)
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("applets/wxAppletsList.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	// 添加
	table_add(modal){
		this.formType = 'new';
		$("#appletsFrom").data('bootstrapValidator').resetForm();
		modal.show();
	}
	// 关闭弹窗
	appletsClose(modal){
		modal.hide();
		this.appletsFrom.id = '';
		this.appletsFrom.describe = '';
		this.appletsFrom.configJson = '';
		this.appletsFrom.appletsName = '';
	}
	// 提交弹窗
	appletsSubmit(modal){
		$('#appletsFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#appletsFrom').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			this.jsonApiService.http_obj("applets/editApplets.html",this.appletsFrom).subscribe(data=> {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
					this.appletsClose(modal);
				}
			})
		}
	}
	// 编辑弹窗
	table_edit(row,modal){
		this.formType = 'updata';
		$("#appletsFrom").data('bootstrapValidator').resetForm();
		this.appletsFrom.id = row.id;
		this.appletsFrom.describe = row.describe;
		this.appletsFrom.configJson = row.configJson;
		this.appletsFrom.appletsName = row.appletsName;
		modal.show();
	}
	// 删除数据
	table_del(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("applets/delApplets.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
}